package com.xydtech.ecard.ftp.service.impl;

import com.xydtech.common.utils.DateUtils;
import com.xydtech.ecard.ftp.domain.EcardApiFtpInfo;
import com.xydtech.ecard.ftp.domain.EcardFtpDeadlineMatchBO;
import com.xydtech.ecard.ftp.domain.FtpDepositLoanRateConfig;
import com.xydtech.ecard.ftp.enums.*;
import com.xydtech.ecard.ftp.service.IFtpDepositLoanRateConfigService;
import com.xydtech.ecard.ftp.service.IFtpPrincipalRepaymentPeriodService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;

/**
 * @Author: liliya
 * @Description: 还款方式:到期还本定期结息
 * @Date: Created in 2023/8/30 18:16
 * @Modified:
 */
@Service
public class FtpPrincipalRepaymentPeriodServiceImpl implements IFtpPrincipalRepaymentPeriodService {

    private final Logger logger = LogManager.getLogger(this.getClass());

    private IFtpDepositLoanRateConfigService ftpDepositLoanRateConfigService;
    public FtpPrincipalRepaymentPeriodServiceImpl(IFtpDepositLoanRateConfigService ftpDepositLoanRateConfigService) {
        this.ftpDepositLoanRateConfigService = ftpDepositLoanRateConfigService;
    }

    /**
     * 计算基础价格
     * @param ecardApiFtpInfo
     * @return
     */
    @Override
    public EcardFtpDeadlineMatchBO getCalculateBasicPrice(EcardApiFtpInfo ecardApiFtpInfo) {

        EcardFtpDeadlineMatchBO ecardFtpDeadlineMatchBO = new EcardFtpDeadlineMatchBO();
        /**
         * 业务起始日(EcardApiFtpInfo-业务起始日)
         * 日期格式必须为：YYYY-MM-DD
         */

        ecardFtpDeadlineMatchBO.setBeginDate(ecardApiFtpInfo.getBeginDate());
        String localBeginDateStr = DateUtils.parseDateToStr("yyyy-MM-dd",ecardFtpDeadlineMatchBO.getBeginDate());
        LocalDate localBeginDate = LocalDate.parse(localBeginDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        /**
         * 业务到期日(EcardApiFtpInfo-业务到期日)
         * 日期格式必须为：YYYY-MM-DD
         */
        ecardFtpDeadlineMatchBO.setEndDate(ecardApiFtpInfo.getEndDate());
        String localEndDateStr = DateUtils.parseDateToStr("yyyy-MM-dd",ecardFtpDeadlineMatchBO.getEndDate());
        LocalDate localEndDate = LocalDate.parse(localEndDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        /**
         * 重定价周期(EcardApiFtpInfo-重定价频率(月))
         */
        Integer repricingFrequencyType = ecardApiFtpInfo.getRepricingFrequencyType();
        if(repricingFrequencyType == null || ecardApiFtpInfo.getInterestRateType() == FtpInterestRateTypeEnum.FIXED_RATE.getValue()){
            /**
             * 如果在前面未设置重定价频率,那么这里设置为0,为了后续"重定价日"好计算.
             */
            ecardApiFtpInfo.setRepricingFrequencyType(0);
        }
        ecardFtpDeadlineMatchBO.setRepricingPeriod(ecardApiFtpInfo.getRepricingFrequencyType());
        /**
         * 重定价日
         */
        Date repricingDate = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),ecardFtpDeadlineMatchBO.getRepricingPeriod());
        String localRepricingDateStr = DateUtils.parseDateToStr("yyyy-MM-dd",repricingDate);
        LocalDate localRepricingDate = LocalDate.parse(localRepricingDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String localBeginDateStrNew = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localRepricingDate).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        ecardFtpDeadlineMatchBO.setRepricingDate(DateUtils.parseDateForDate(localBeginDateStrNew));
        /**
         * 定价期限
         */
        Integer pricingPeriod = 0;
        Integer interestRateType = ecardApiFtpInfo.getInterestRateType();
        Date calculateDate1 = null;
        Date calculateDate2 = null;
        if(interestRateType == FtpInterestRateTypeEnum.FIXED_RATE.getValue()){
            calculateDate1 = ecardFtpDeadlineMatchBO.getBeginDate();
            calculateDate2 = ecardFtpDeadlineMatchBO.getEndDate();
            pricingPeriod = DateUtils.differentDaysByActualMillisecond(calculateDate1,calculateDate2);
        }else if(interestRateType == FtpInterestRateTypeEnum.FLOAT_RATE.getValue()){
            calculateDate1 = ecardFtpDeadlineMatchBO.getBeginDate();
            calculateDate2 = ecardFtpDeadlineMatchBO.getRepricingDate();
            pricingPeriod = DateUtils.differentDaysByActualMillisecond(calculateDate1,calculateDate2);
        }
        ecardFtpDeadlineMatchBO.setPricingPeriod(pricingPeriod);

        //根据日期差值求下限
        Map<String,Integer> getDiffYearMonth01 = DateUtils.getDateDetails(calculateDate1,calculateDate2);
        Integer getDiffYearMonth01Year = getDiffYearMonth01.get("years");
        Integer getDiffYearMonth01Month = getDiffYearMonth01.get("month");
        Integer getDiffYearMonth01Days = getDiffYearMonth01.get("days");
        String getDiffYearMonth01ConfigKey = null;

        if(getDiffYearMonth01Year > 0){
            FtpDepositLoanRateConfigKeyYearTypeEnum enums = FtpDepositLoanRateConfigKeyYearTypeEnum.valuesByNumber(getDiffYearMonth01Year);
            if(enums != null){
                getDiffYearMonth01ConfigKey = enums.getConfigKey();
            }else{
                getDiffYearMonth01ConfigKey = FtpDepositLoanRateConfigKeyEnum.Y_30.getConfigKey();
            }

        }else if(getDiffYearMonth01Year == 0 && getDiffYearMonth01Month > 0){
            getDiffYearMonth01ConfigKey = FtpDepositLoanRateConfigKeyMonthTypeEnum.valuesByNumber(getDiffYearMonth01Month).getConfigKey();
        }else if(getDiffYearMonth01Year == 0 && getDiffYearMonth01Month == 0 && getDiffYearMonth01Days > 0){
            getDiffYearMonth01ConfigKey = FtpDepositLoanRateConfigKeyDaysTypeEnum.valuesByNumber(getDiffYearMonth01Days).getConfigKey();
        }
        //通过期限匹配法-查对应下限区间
        FtpDepositLoanRateConfig ftpDepositLoanRateConfigUnder =
                ftpDepositLoanRateConfigService.selectBaseListByConfigKey(getDiffYearMonth01ConfigKey,FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());
        /**
         * 下限
         */
        Integer configUnderDay = null;
        /**
         * 下限收益率
         */
        BigDecimal configUnderRate;
        /**
         * 上限
         */
        Integer configUpperDay = null;
        //上限KEY类型
        String configUpperKey = null;
        //TODO 太多if了得改
        if(ftpDepositLoanRateConfigUnder != null){

            /**
             * 下限收益率
             */
            configUnderRate = new BigDecimal(ftpDepositLoanRateConfigUnder.getConfigRate());
            /**
             * 上限
             */
            Date calculateConfigUpperDay = null;
            LocalDate localCalculateConfigUpperDay = null;
            if(ecardApiFtpInfo.getInterestRateType() == FtpInterestRateTypeEnum.FLOAT_RATE.getValue()){
                /**
                 * 下限
                 */
                configUnderDay = getDiffYearMonth01Days;
                if(ecardFtpDeadlineMatchBO.getRepricingPeriod() == FtpRepricingFrequencyTypeEnum.FREQUENCY_3M.getValue()){
                    //calculateConfigUpperDay = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),6);

                    localCalculateConfigUpperDay = localBeginDate.plusMonths(6);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.M_6.getConfigKey();
                }else if(ecardFtpDeadlineMatchBO.getRepricingPeriod() == FtpRepricingFrequencyTypeEnum.FREQUENCY_6M.getValue()){
                    //calculateConfigUpperDay = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),9);

                    localCalculateConfigUpperDay = localBeginDate.plusMonths(9);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.M_9.getConfigKey();
                }else if(ecardFtpDeadlineMatchBO.getRepricingPeriod() == FtpRepricingFrequencyTypeEnum.FREQUENCY_12M.getValue()){
                    //calculateConfigUpperDay = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),24);

                    localCalculateConfigUpperDay = localBeginDate.plusMonths(24);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_2.getConfigKey();
                }

                localCalculateConfigUpperDay = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localCalculateConfigUpperDay);
                configUpperDay = (int)localBeginDate.until(localCalculateConfigUpperDay,ChronoUnit.DAYS);
                //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),calculateConfigUpperDay);

            }else if(ecardApiFtpInfo.getInterestRateType() == FtpInterestRateTypeEnum.FIXED_RATE.getValue()){
                String configKey = ftpDepositLoanRateConfigUnder.getConfigKey();
                if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.N_0.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextDay(ecardFtpDeadlineMatchBO.getBeginDate(),1);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextDay(ecardFtpDeadlineMatchBO.getBeginDate(),7);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusDays(1));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusDays(7));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.W_1.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.W_1.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextDay(ecardFtpDeadlineMatchBO.getBeginDate(),7);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextDay(ecardFtpDeadlineMatchBO.getBeginDate(),14);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusDays(7));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusDays(14));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.W_2.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.W_2.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextDay(ecardFtpDeadlineMatchBO.getBeginDate(),14);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),1);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusDays(14));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(1));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.M_1.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.M_1.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),1);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),3);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(1));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(3));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.M_3.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.M_3.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),3);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),6);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(3));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(6));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.M_6.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.M_6.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),6);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),9);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(6));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(9));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.M_9.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.M_9.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),9);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),12);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(9));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(12));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.M_9.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_1.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),12);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),24);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(12));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(24));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_2.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_2.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),24);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),36);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(24));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(36));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_3.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_3.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),36);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),60);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(36));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(60));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_5.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_5.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),60);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),120);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(60));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(120));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_10.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_10.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),120);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),180);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(120));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(180));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_15.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_15.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),180);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),240);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(180));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(240));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_20.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_20.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),240);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),360);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(240));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(360));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_30.getConfigKey();
                }else if(configKey.equals(FtpDepositLoanRateConfigKeyEnum.Y_30.getConfigKey())){

                    //Date diffDaysDate01 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),360);
                    //configUnderDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate01);
                    //Date diffDaysDate02 = DateUtils.getNextMonth(ecardFtpDeadlineMatchBO.getBeginDate(),960);
                    //configUpperDay = DateUtils.differentDaysByActualMillisecond(ecardFtpDeadlineMatchBO.getBeginDate(),diffDaysDate02);

                    LocalDate diffDaysDate01 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(360));
                    configUnderDay = (int)localBeginDate.until(diffDaysDate01,ChronoUnit.DAYS);
                    LocalDate diffDaysDate02 = DateUtils.dateInitCheckFtpDataProcess(localBeginDate,localBeginDate.plusMonths(960));
                    configUpperDay = (int)localBeginDate.until(diffDaysDate02,ChronoUnit.DAYS);

                    //TODO 30取自身的默认值
                    configUpperKey = FtpDepositLoanRateConfigKeyEnum.Y_30.getConfigKey();
                }
            }

        }else{
            configUnderDay = 0;
            configUnderRate = new BigDecimal("0");
            if(configUpperDay == null){
                configUpperDay = 0;
            }
        }
        ecardFtpDeadlineMatchBO.setConfigUnderDay(configUnderDay);
        ecardFtpDeadlineMatchBO.setConfigUnderRate(configUnderRate);
        ecardFtpDeadlineMatchBO.setConfigUpperDay(configUpperDay);
        /**
         * 上限收益率
         */
        FtpDepositLoanRateConfig ftpDepositLoanRateConfigUpper = ftpDepositLoanRateConfigService.selectBaseListByConfigKey(configUpperKey,FtpPricingParamTypeEnum.DEPOSIT_LOAD_YIELD_CURVE.getValue());

        BigDecimal configUpperRate;
        if(ftpDepositLoanRateConfigUpper != null){
            configUpperRate = new BigDecimal(ftpDepositLoanRateConfigUpper.getConfigRate());
        }else{
            configUpperRate = new BigDecimal("0");
        }
        ecardFtpDeadlineMatchBO.setConfigUpperRate(configUpperRate);
        /**
         * 边线价格
         * 上限收益率 == 下限收益率 ? 上限收益率 : 0
         */
        BigDecimal marginalPrice;
        if(configUnderRate.compareTo(configUpperRate) == 0){
            //TODO 待验证是否可行
            marginalPrice = ecardFtpDeadlineMatchBO.getConfigUpperRate();
        }else{
            marginalPrice = new BigDecimal("0");
        }
        ecardFtpDeadlineMatchBO.setMarginalPrice(marginalPrice);

        /**
         * 斜率
         * (上限收益率 - 下限收益率)/(上限 - 下限)
         */
        BigDecimal slopeSubstract01 = new BigDecimal(String.valueOf(configUpperDay - configUnderDay));
        BigDecimal slopeSubstract02 = configUpperRate.subtract(configUnderRate);
        //TODO 待定
        BigDecimal slope = slopeSubstract02.divide(slopeSubstract01,9,BigDecimal.ROUND_HALF_UP);
        ecardFtpDeadlineMatchBO.setSlope(slope);
        /**
         * 截距
         * 上限收益率 - (斜率 * 上限)
         */
        BigDecimal intercept = ecardFtpDeadlineMatchBO.getConfigUpperRate()
                .subtract(
                        ecardFtpDeadlineMatchBO.getSlope().multiply(new BigDecimal(ecardFtpDeadlineMatchBO.getConfigUpperDay()))
                ).setScale(6,BigDecimal.ROUND_HALF_UP);
        ecardFtpDeadlineMatchBO.setIntercept(intercept);
        /**
         * 插值价格
         * 定价期限 * 斜率 + 截距
         */
        BigDecimal pricingPeriodDecimal = new BigDecimal(String.valueOf(ecardFtpDeadlineMatchBO.getPricingPeriod()));
        BigDecimal interpolationPrice = pricingPeriodDecimal.multiply(ecardFtpDeadlineMatchBO.getSlope())
                .add(ecardFtpDeadlineMatchBO.getIntercept()).setScale(4,BigDecimal.ROUND_HALF_UP);
        ecardFtpDeadlineMatchBO.setInterpolationPrice(interpolationPrice);
        /**
         * 基础价格
         * 边线价格 == 0 ? 插值价格 : 边线价格
         */
        BigDecimal basicPrice;
        BigDecimal zeroDecimal = new BigDecimal("0");
        if(marginalPrice.compareTo(zeroDecimal) == 0){
            basicPrice = interpolationPrice;
        }else{
            basicPrice = marginalPrice;
        }
        ecardFtpDeadlineMatchBO.setBasicPrice(basicPrice);

        return ecardFtpDeadlineMatchBO;

    }


    public IFtpDepositLoanRateConfigService getFtpDepositLoanRateConfigService() {
        return ftpDepositLoanRateConfigService;
    }

    public void setFtpDepositLoanRateConfigService(IFtpDepositLoanRateConfigService ftpDepositLoanRateConfigService) {
        this.ftpDepositLoanRateConfigService = ftpDepositLoanRateConfigService;
    }
}
